package com.party.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.party.domain.Signin;
import com.party.domain.User;
import com.party.domain.UserRole;




@Mapper
public interface UserMapper 
{
	@Insert("INSERT INTO User(id,email,password,name,state,code,integral,phone,regdate,icon) VALUES(#{id},#{email},#{password},#{name},#{state},#{code},#{integral},#{phone},#{time},#{icon})")
	@Options(useGeneratedKeys=true, keyProperty="id")
	void inster(User user);
	
	@Insert("INSERT INTO User(Id,email,password,name,state,code,integral,phone,regdate,icon) VALUES(#{id},#{email},#{password},#{name},#{state},#{code},#{integral},#{Phone},#{time},#{icon})")
	@Options(useGeneratedKeys=true, keyProperty="id")
	void insterAdmin(User user);
	
	@Insert("INSERT INTO User_Role(ruid,role_id,user_id) VALUES(#{id},#{role_id},#{user_id})")
	void insterUR(UserRole ur);
	
	//分页查询所有用户
	@Select("SELECT * FROM User ORDER BY Id DESC LIMIT #{start},#{size}")
	@ResultMap("com.party.mapper.UserMapper.UserBaseMapped")
	List<User> selectAllUser(@Param("start")int start,@Param("size")int size);
	
	@Select("SELECT * FROM User WHERE email = #{email}")
	@ResultMap("com.party.mapper.UserMapper.UserBaseMapped")
	List<User> selectUser(@Param("email") String email);
	
	@Select("SELECT email,phone,name,integral,regdate,icon FROM User WHERE Id = #{id}")
	User selectUserMessage(@Param("id") int id);
	
	@Select("SELECT * FROM User WHERE Id = #{id}")
	@ResultMap("com.party.mapper.UserMapper.UserBaseMapped")
	User selectIdUserMessage(@Param("id") int id);
	
	
	@Update("UPDATE User SET code=1 WHERE email=#{email}")
	void update(String email);
	
	@Update("UPDATE User SET password=#{password} WHERE email=#{email}")
	void updateIdUser(@Param("email") String email,@Param("password")String password);
	
	@Update("UPDATE User SET icon=#{icon} WHERE Id=#{id}")
	void updateIcon(/*int id,*/User user);
	
	
	//签到
	@Insert("INSERT INTO Signin(siid,user_id,sign_time) VALUES(#{id},#{user_id},#{time})")
	void insterSignin(Signin sigin);
	
	@Select("SELECT * FROM Signin WHERE user_id = #{id}")
	@Results({
		@Result(property = "time",  column = "sign_time", javaType = java.util.Date.class)
	})
	Signin selectSignin(@Param("id") int id);
	
	@Update("UPDATE Signin SET sign_time=#{time} WHERE user_id=#{user_id}")
	void updateTime(Signin signin);
	
	@Update("UPDATE User SET integral=#{integral} WHERE Id=#{id}")
	void updateIntegral(User user);
	
	@Update("UPDATE User SET phone=#{phone},name=#{name} WHERE Id=#{id}")
	void updateUserDate(User user);
	
	//管理员更新用户所有数据
	@Update("UPDATE User SET email=#{email},password=#{password},phone=#{Phone},name=#{name},state=#{state},code=#{code},integral=#{integral},regdate=#{time},icon=#{icon} WHERE Id=#{id}")
	void updateUserAllDate(User user);
	
	//更新用户权限
	@Update("UPDATE User_Role SET role_id=#{role_id} WHERE user_id=#{uid}")
	void updateUserRole(@Param("role_id") int role_id,@Param("uid")int uid);
}
